home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Documentation / Books / Learn Java on the Macintosh / Learn Java Projects / 11.06 - payroll / Payroll.java < prev    next >
Text File  |  1996-04-22  |  2KB  |  89 lines

  1. /* -----------------------------------------------------------
  2.    This illustrates the beginning of an applet to keep track
  3.    of employees in a database. This version defines an
  4.    Employee class but only adds the text fields to the applet
  5.    for use once more of the applet is developed. 
  6.    
  7.    Java's classes: Applet     (applet)  
  8.                    TextField  (awt)     for entering new employee data
  9.                    Label      (awt)     read-only text
  10.                    GridLayout (awt)     aligns by columns and rows
  11.                    
  12.    Custom classes: Payroll
  13.                    Employee             payroll information
  14.                    
  15. ----------------------------------------------------------- */
  16. import java.applet.Applet;
  17. import java.awt.*;
  18.  
  19. public class Payroll extends Applet {
  20.     TextField  textFieldEmployee;
  21.     TextField  textFieldWage;
  22.     TextField  textFieldHours;
  23.     Label      labelEarned;
  24.    
  25.     /* Create user interface needed by this applet. */
  26.     public void init() {
  27.     
  28.         // Arrange the user interface in a grid.
  29.         setLayout(new GridLayout(4,2)); // 4 rows, 2 columns
  30.       
  31.         // 1st row
  32.         add(new Label("Employee number:"));
  33.         textFieldEmployee = new TextField(20); // 20 columns wide
  34.         add(textFieldEmployee);
  35.       
  36.         // 2nd row
  37.         add(new Label("Hourly wage:"));
  38.         textFieldWage = new TextField(20); // 20 columns wide
  39.         add(textFieldWage);
  40.       
  41.         // 3rd row
  42.         add(new Label("Hours worked:"));
  43.         textFieldHours = new TextField(20); // 20 columns wide
  44.         add(textFieldHours);
  45.       
  46.         // 4th row
  47.         add(new Label("Earned income:"));
  48.         labelEarned = new Label(); 
  49.         add(labelEarned);
  50.        }
  51.     
  52.     /** Detect keyboard entry. */
  53.     public boolean action(Event e, Object arg) {
  54.             
  55.         if (e.target == textFieldEmployee) {
  56.             
  57.             System.out.println("Employee number");
  58.                     
  59.         } else if (e.target == textFieldWage) {
  60.             
  61.             System.out.println("Hourly wage");
  62.                 
  63.         } else if (e.target == textFieldHours) {
  64.         
  65.             System.out.println("Hours worked");
  66.  
  67.         }
  68.         
  69.         return super.action(e, arg);
  70.     }
  71.  
  72.     
  73. }
  74.  
  75. /** Maintain payroll information for an employee. */
  76. class Employee {
  77.     int idNumber;
  78.     int hourlyWage;
  79.     int hoursWorked;
  80.     
  81.     int earnedIncome() {
  82.         return hourlyWage * hoursWorked;
  83.     }
  84. }
  85.  
  86.  
  87.  
  88.  
  89.